//
//  HCStatusOriginalView.swift
//  Hweibome
//
//  Created by 何川 on 15/12/10.
//  Copyright © 2015年 hechuan. All rights reserved.
//

import UIKit
import SnapKit

class HCStatusOriginalView: UIView {
    
    var bottomConstraint : Constraint?

    var statusViewModel: HCStatusViewModel? {
        didSet{
            // 设置头像
            iconView.sd_setImageWithURL(NSURL(string: (statusViewModel?.status?.user?.profile_image_url)!), placeholderImage: UIImage(named: "avatar_default_big"))
            // 设置昵称
            nameLabel.text = statusViewModel?.status?.user?.name
            
            // TODO: 设置创建时间,先写死, 后面会做特殊处理
            timeLabel.text = statusViewModel?.createAtText
            sourceLabel.text = statusViewModel?.sourceText
            // 设置会员图标以及认证
            vipImageView.image = statusViewModel?.vipImage
            verifiedImageView.image = statusViewModel?.verifiedImage
            contentLabel.text = statusViewModel?.status?.text
            
            bottomConstraint?.uninstall()
            
            if let pic_urls = statusViewModel?.status?.pic_urls where pic_urls.count > 0{
                pictureView.pic_urls = pic_urls
                
                pictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictureView).offset(HCStatusCellMargin).constraint
                })
                
            } else{
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contentLabel).offset(HCStatusCellMargin).constraint
                })
            }
            //没有1.隐藏控件,2 更新约束
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   
    
    private func setupUI(){
        
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(vipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 2. 添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(HCStatusCellMargin)
            make.leading.equalTo(self).offset(HCStatusCellMargin)
            make.size.equalTo(CGSizeMake(35, 35))
        }
        // 昵称的label
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(HCStatusCellMargin)
        }
        // 会员图标
        vipImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(HCStatusCellMargin)
        }
        // 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(nameLabel)
        }
        // 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(HCStatusCellMargin)
        }
        // 认证
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        // 原创微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView)
            //            make.trailing.equalTo(self).offset(-HMStatusCellMargin)
            make.top.equalTo(iconView.snp_bottom).offset(HCStatusCellMargin)
           

        }
        // 配图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(HCStatusCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        
        // 关键
        snp_makeConstraints { (make) -> Void in
            // make.bottom.equalTo(contentLabel).offset(HCStatusCellMargin)
            self.bottomConstraint = make.bottom.equalTo(pictureView).offset(HCStatusCellMargin).constraint
        }
        
    }
   
    
     //MARK: - 数据的懒加载
    
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    private lazy var nameLabel:UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: HCStatusNameFontSize )
    private lazy var timeLabel:UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: HCStatusTimeFontSize)
    private lazy var sourceLabel:UILabel = UILabel(textColor: UIColor.lightGrayColor(), fontSize: HCStatusTimeFontSize)
    private lazy var vipImageView:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    private lazy var verifiedImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    private lazy var contentLabel:UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: HCStatusContentFontSize,maxWidth: SCREENW - 2*HCStatusCellMargin)
    
    private lazy var pictureView : HCStatusPictureView = {
        let view = HCStatusPictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
    
}


    
